package kyssion.leetcode.num151_200;

public class code162_寻找峰值 {

    public static void main(String[] args) {
        System.out.println(new code162_寻找峰值().findPeakElement(
                new int[]{
                        2,1
                }
        ));
    }

    /**
     * 这一题其实很简单
     *
     * @param nums
     * @return
     */
    public int findPeakElement(int[] nums) {
        if (nums == null || nums.length == 0||nums.length == 1) {
            return 0;
        }
        int start = 0;
        int end = nums.length - 1;

        if (nums[start] > nums[start + 1]) {
            return start;
        }

        if (nums[end] > nums[end - 1]) {
            return end;
        }

        while (true) {
            int midle = (start + end) >> 1;
            if (nums[midle] > nums[midle - 1] && nums[midle] > nums[midle + 1]) {
                return midle;
            } else if (nums[midle] < nums[midle - 1] && nums[midle] < nums[midle - 1]) {
                end = midle;
            } else if (nums[midle] > nums[midle - 1] && nums[midle] < nums[midle + 1]) {
                start = midle;
            } else {
                start = midle;
            }
        }
    }
}
